package com.hawk.eye.server.controller.hawkeye;

import com.hawk.eye.common.core.ApiResult;
import com.hawk.eye.framework.utils.CollectionUtil;
import com.hawk.eye.common.page.PageQuery;
import com.hawk.eye.common.page.PageResult;
import com.hawk.eye.server.convert.DictConvert;
import com.hawk.eye.server.convert.RepairApiConvert;
import com.hawk.eye.server.convert.ServiceProblemConvert;
import com.hawk.eye.server.domain.dto.resp.ProblemRecordRespDTO;
import com.hawk.eye.server.domain.dto.resp.RepairProblemRespDTO;
import com.hawk.eye.server.domain.vo.req.DictReqVO;
import com.hawk.eye.server.domain.vo.req.ProblemRecordReqVO;
import com.hawk.eye.server.domain.vo.req.RepairProblemReqVO;
import com.hawk.eye.server.domain.vo.resp.ProblemRecordRespVO;
import com.hawk.eye.server.domain.vo.resp.RepairProblemRespVO;
import com.hawk.eye.server.service.IServiceProblemService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 缺陷管理 Controller
 * @author: salad
 * @date: 2023/9/30
 **/
@Slf4j
@RestController
@RequestMapping("/hawk-eye/problem-record/")
@RequiredArgsConstructor
public class ServiceProblemController {

    private final IServiceProblemService problemService;


    /**
     * 分页查询缺陷管理
     * @param pageQuery
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:problem-record:query')")
    @GetMapping("/page")
    public PageResult<ProblemRecordRespVO> page(PageQuery pageQuery, ProblemRecordReqVO reqVO) {
        PageResult<ProblemRecordRespDTO> pageResult = problemService.page(pageQuery, ServiceProblemConvert.INSTANCE.to(reqVO));
        return PageResult.of(pageResult, ServiceProblemConvert.INSTANCE::to);
    }

    /**
     * 创建缺陷管理
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:problem-record:add')")
    @PostMapping("/create-log-problem")
    public ApiResult createLogProblem(@RequestBody @Validated ProblemRecordReqVO reqVO) {
        boolean status = problemService.createLogProblem(ServiceProblemConvert.INSTANCE.to(reqVO));
        return ApiResult.status(status);
    }

    /**
     * 修改缺陷管理
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:problem-record:edit')")
    @PutMapping("/update")
    public ApiResult update(@RequestBody @Validated ProblemRecordReqVO reqVO) {
        boolean status = problemService.update(ServiceProblemConvert.INSTANCE.to(reqVO));
        return ApiResult.status(status);
    }

    /**
     * 缺陷管理详情
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:problem-record:query')")
    @GetMapping(value = "detail/{id}")
    public ApiResult detail(@PathVariable("id") Long id) {
        return ApiResult.ok(ServiceProblemConvert.INSTANCE.to(problemService.detail(id)));
    }

    /**
     * 缺陷与日志详情
     * @param baseAlarmId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:problem-record:query')")
    @GetMapping(value = "detail-by-log/{baseAlarmId}")
    public ApiResult detailByLog(@PathVariable("baseAlarmId") Long baseAlarmId) {
        return ApiResult.ok(problemService.detailByLog(baseAlarmId));
    }

    /**
     * 激活缺陷
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:problem-record:edit')")
    @PutMapping("/activate/{ids}")
    public ApiResult activate(@PathVariable Long[] ids) {
        boolean status = problemService.activate(ids);
        return ApiResult.status(status);
    }

    /**
     * 解决缺陷
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:problem-record:edit')")
    @PutMapping("/resolve/{ids}")
    public ApiResult resolve(@PathVariable Long[] ids) {
        boolean status = problemService.resolve(ids);
        return ApiResult.status(status);
    }

    /**
     * 关闭缺陷
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:problem-record:edit')")
    @PutMapping("/close/{ids}")
    public ApiResult close(@PathVariable Long[] ids) {
        boolean status = problemService.close(ids);
        return ApiResult.status(status);
    }


    /**
     * 删除缺陷
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:problem-record:remove')")
    @DeleteMapping("/remove/{ids}")
    public ApiResult remove(@PathVariable Long[] ids) {
        return ApiResult.status(problemService.removeBatchByIds(CollectionUtil.toList(ids)));
    }

    /**
     * @param pageQuery
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:repair:query')")
    @GetMapping("/repair-problem-page")
    public PageResult<RepairProblemRespVO> repairProblemPage(PageQuery pageQuery, RepairProblemReqVO reqVO) {
        PageResult<RepairProblemRespDTO> pageResult = problemService.repairProblemPage(pageQuery, RepairApiConvert.INSTANCE.to(reqVO));
        return PageResult.of(pageResult, ServiceProblemConvert.INSTANCE::to);
    }

    /**
     * 缺陷字典列表
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:problem-record:query')")
    @GetMapping(value = "/problem-dict-list")
    public ApiResult problemDictList() {
        return ApiResult.ok(DictConvert.INSTANCE.to6(problemService.problemDictList()));
    }

    /**
     *
     * @param type
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:problem-record:query')")
    @GetMapping(value = "/dict-list-by-type/{type}")
    public ApiResult dictListByType(@PathVariable("type") String type) {
        return ApiResult.ok(DictConvert.INSTANCE.to6(problemService.dictListByType(type)));
    }

    /**
     *
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:problem-record:add')")
    @PostMapping("/save-dict")
    public ApiResult saveDict(@RequestBody @Validated DictReqVO reqVO) {
        boolean status = problemService.saveDict(DictConvert.INSTANCE.to(reqVO));
        return ApiResult.status(status);
    }

    /**
     *
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:problem-record:edit')")
    @PutMapping("/update-dict")
    public ApiResult updateDict(@RequestBody @Validated DictReqVO reqVO) {
        boolean status = problemService.updateDict(DictConvert.INSTANCE.to(reqVO));
        return ApiResult.status(status);
    }

    /**
     *
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:problem-record:remove')")
    @DeleteMapping("/remove-dict/{ids}")
    public ApiResult removeDict(@PathVariable Long[] ids) {
        return ApiResult.status(problemService.removeDict(ids));
    }


}
